package com.zmn.brs.admin.controller;

import com.zmn.common.exception.ZmnException;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.cas.web.StaffAdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 类描述：BRS基础Controller
 * @author zhangyao
 * @since 2021/05/10 16:30
 */
public class BrsBaseController {

    private static final Logger logger = LoggerFactory.getLogger(BrsBaseController.class);

    /**
     * 默认排序值
     */
    protected static final int DEFAULT_SORT = 100;
    /**
     * 登录白名单缓存时间
     */
    protected static final int BLACK_CATCH_TIME = 120;
    /**
     * 导出excel没有配置限制数量导出失败errorCode
     */
    protected static final int EXPORT_LIMIT_ERROR_CODE = 555;

    /**
     * 获取登录用户
     *
     * @return
     */
    public LoginStaff getMcStaff() {
        return StaffAdminService.currentStaff();
    }

    /**
     * 获取登录用户ID
     *
     * @param
     * @return
     */
    public int getMcStaffId() {
        LoginStaff staff = StaffAdminService.currentStaff();
        if(Objects.nonNull(staff)){
            return staff.getStaffId();
        }else {
            return 0;
        }
    }

    /**
     * 获取登录用户名
     *
     * @param
     * @return
     */
    public String getMcStaffRealName() {
        LoginStaff staff = StaffAdminService.currentStaff();
        if(Objects.nonNull(staff)){
            return staff.getRealName();
        }else {
            return "";
        }
    }

    /**
     * 获取登录姓名
     *
     * @return
     */
    public String getMcStaffName() {
        LoginStaff staff = StaffAdminService.currentStaff();
        if(Objects.nonNull(staff)){
            return staff.getStaffName();
        }else {
            return "";
        }
    }

    protected Integer getExportPageLimit(HttpServletResponse response) {
        Integer pageSize = getMcStaff().getExportLimit();
        if (NumberUtil.isNullOrZero(pageSize)) {
            try {
                response.setContentType("text/html;charset=utf-8");
                response.setStatus(EXPORT_LIMIT_ERROR_CODE);
                response.getWriter().print(EXPORT_LIMIT_ERROR_CODE);
                throw new ZmnException("未配置导出数量限制，导出失败");
            } catch (IOException e) {
                logger.error("", e);
            }
        }
        return pageSize;
    }

}
